SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE dbo.ICOverlap AS
BEGIN
DECLARE @FundId VARCHAR(5) = 'FSIC'
DECLARE @ExportId INT = (SELECT MAX(e.ExportId) FROM  FSFundModel.reporting.Export e WHERE e.FundId = @FundId)


;WITH FSIC1 AS (

SELECT * FROM FSFundModel.reporting.Asset AS a WHERE a.ExportId = @ExportId
 AND a.IsActiveEndOfDay =1
 ),
 fsic2 AS (
			SELECT * FROM FSFundModel.reporting.Asset AS a WHERE a.ExportId =(SELECT MAX(e.ExportId) FROM  FSFundModel.reporting.Export e WHERE e.FundId = 'fsic2')
	
		  ),
DISt AS (SELECT DISTINCT FSIC1.InstrumentId FROM  FSIC1
INNER JOIN fsic2 ON FSIC1.InstrumentId = fsic2.InstrumentId

          )
SELECT  DISt.InstrumentId ,
        i.InstrName FROM DISt

INNER JOIN FSFundModel.dbo.Instrument AS i ON DISt.InstrumentId = i.InstrumentId
END

GO
